package reflect;

import domin.Student;
/*
获取成员方法
 */
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Demo4 {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class<Student> studentClass = Student.class;
        //反射多个（public修饰的）
        Method[] methods = studentClass.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }

        //反射单个指定的方法
        Method eatMethod1 = studentClass.getMethod("eat");
        System.out.println(eatMethod1);

        Method eatMethod2 = studentClass.getMethod("eat", int.class);
        System.out.println(eatMethod2);

        Student stu = studentClass.getConstructor().newInstance();
        eatMethod2.invoke(stu,3);

    }
}
